package com.darco.darcoapp;

import java.util.Calendar;
import java.util.Date;

import com.darco.darcoapp.interfaces.IGeoLocatingActivity;

import eneter.messaging.endpoints.typedmessages.TypedResponseReceivedEventArgs;
import DARCOShared.requests.GetCardInfoRequest;
import DARCOShared.requests.RequestBase;
import DARCOShared.requests.UpdateUserPositionRequest;
import DARCOShared.responses.GetCardInfoResponse;
import DARCOShared.responses.ResponseBase;
import DARCOShared.responses.ResponseWrapper;
import DARCOShared.utils.MD5;
import DARCOShared.utils.SecurityUtilities;
import static DARCOShared.utils.Constants.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class Test_Activity extends MessagingActivity implements OnClickListener, IGeoLocatingActivity {
	private MessagingManager mm;
	private GeolocationHelper glh;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_);

		glh = new GeolocationHelper(this);

		//set onclick listener
		View req_button= findViewById(R.id.test_request_button);
		req_button.setOnClickListener(this);

		findViewById(R.id.test_bluetooth_scan).setOnClickListener(this);
		findViewById(R.id.test_bluetooth_listen).setOnClickListener(this);
		findViewById(R.id.test_requestinfo_button).setOnClickListener(this);
		findViewById(R.id.test_access_main_button).setOnClickListener(this);
		findViewById(R.id.test_geolocate).setOnClickListener(this);
		findViewById(R.id.test_cardjourney).setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_test_, menu);
		return true;
	}

	public void onPerformRequest(RequestBase rb){
		//inizializzazione ...
		mm = new MessagingManager();
		mm.ConnectToServer(this); //mi connetto al server e mi iscrivo alle risposte
		mm.SendRequest(rb, this); //invia una richiesta di info carta
		//qui sara necessario mostrare un popup di attesa, tipo "caricamento in corso" o roba simile
		Toast.makeText(getApplicationContext(), "Request sent!", Toast.LENGTH_SHORT).show();
	}

	//Qui arrivano le risposte (se sono iscritto)
	@Override
	void onResponseReceived(Object sender, TypedResponseReceivedEventArgs<ResponseWrapper> e) {
		//E' arrivata la risposta!
		ResponseBase respbase = e.getResponseMessage().getResponse();
		switch(respbase.getResponseType()){
		case GetCardInfo :
			GetCardInfoResponse resp = (GetCardInfoResponse) respbase;
			Toast.makeText(getApplicationContext(), "Response received!", Toast.LENGTH_SHORT).show();
			//do stuff
			break;
		case GetCardCatalog:
			break;
		case GetCardJourney:
			break;
		case GetNearestOwners:
			break;
		case RegisterCard:
			break;
		case UpdateUserPosition:
			break;
		default:
			break;
		}

		mm.CloseConnection(); //chiude la connessione con il server (importante!)
		//Processa la risposta / elabora i dati
		//Spegne il popup di attesa e ritorna il controllo all'utente
	}


	private void onTestBTScan() {
		Intent i3 = new Intent(this,BluetoothManager.class);
		i3.putExtra("mode", BluetoothManager.INTENT_SCAN_AND_SEND);
		i3.putExtra("cardSN", "AZEROTH_LOOT_2_3_THUNDERHEAD_HIPPOGRYPH_WOW");
		i3.putExtra("cardUniqueID", "UNIQUE_CARD_ID_2");
		i3.putExtra("system", getIntent().getExtras().getParcelable("system"));
		i3.putExtra("account", getIntent().getExtras().getParcelable("account"));

		startActivityForResult(i3, BluetoothManager.INTENT_SCAN_AND_SEND);
	}

	private void onTestBTListen() {
		Intent i3 = new Intent(this,BluetoothManager.class);
		i3.putExtra("mode", BluetoothManager.INTENT_LISTEN_AND_RECEIVE);
		i3.putExtra("system", getIntent().getExtras().getParcelable("system"));
		i3.putExtra("account", getIntent().getExtras().getParcelable("account"));
		startActivityForResult(i3, BluetoothManager.INTENT_LISTEN_AND_RECEIVE);
	}
	
	private void onTestCardJourney() {
		Intent i3 = new Intent(this,CardJourneyActivity.class);
		i3.putExtra("system", getIntent().getExtras().getParcelable("system"));
		i3.putExtra("account", getIntent().getExtras().getParcelable("account"));
		i3.putExtra("cardSN", "AZEROTH_LOOT_2_3_THUNDERHEAD_HIPPOGRYPH_WOW");
		i3.putExtra("cardUID", "UNIQUE_CARD_ID_2");

		startActivityForResult(i3, BluetoothManager.INTENT_LISTEN_AND_RECEIVE);
	}

	private void onRequestCardInfo() {
		onPerformRequest(new GetCardInfoRequest("AZEROTH_LOOT_2_3_THUNDERHEAD_HIPPOGRYPH_WOW", "2"));		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.test_request_button:
			onRequestCardInfo();
			break;
		case R.id.test_bluetooth_scan:
			onTestBTScan();
			break;
		case R.id.test_bluetooth_listen:
			onTestBTListen();
			break;
		case R.id.test_requestinfo_button:
			onRequestCardInfo();
			break;
		case R.id.test_access_main_button:{
			Toast.makeText(this, "accesso con test000", Toast.LENGTH_SHORT).show();
			ContentValues account= new ContentValues();
			ContentValues system= new ContentValues();
			//account.put(ACCOUNT_AVATAR,null);
			account.put(ACCOUNT_EMAIL, "test000@gmail.com");
			account.put(ACCOUNT_NAME, "testname000");
			account.put(ACCOUNT_SURNAME, "testsur000");
			account.put(ACCOUNT_ID, 000);
			Calendar time = Calendar.getInstance();
			account.put(ACCOUNT_CREATION, time.getTime().toString());
			account.put(ACCOUNT_USERNAME, "test000");
			account.put(ACCOUNT_PASSWORD, MD5.get("test000"));
			//non penso serva inserirlo anche nel db

			system.put(SYSTEM_ALLOWLOCATION, 1);
			system.put(SYSTEM_SAVEDACCOUNT,"test000@gmail.com");

			Intent i= new Intent(this,Main_Activity.class);
			i.putExtra("account", account);
			i.putExtra("system", system);
			startActivity(i);

			break;
		}
		case R.id.test_geolocate :
			onGeolocate();
			break;
		case R.id.test_cardjourney :
			onTestCardJourney();
			break;
		default:
			break;
		}

	}

	private void onGeolocate() {
		Intent i3 = new Intent(this, UpdateLocation_Activity.class);
		i3.putExtra("system", getIntent().getExtras().getParcelable("system"));
		i3.putExtra("account", getIntent().getExtras().getParcelable("account"));

		startActivityForResult(i3, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

	}

	@Override
	public void onLocationObtained() {
		hideLoadingDialog();
		Toast.makeText(getApplicationContext(), "Location found:\nLAT = " + glh.getLatitude() + "\nLONG = " + glh.getLongitude(), Toast.LENGTH_LONG).show();
	}

}
